﻿using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;


public partial class IFRS_WIN16_2E : PageBase
{
    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {         
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN16_1S.aspx");
    } 
    #endregion "---------- Events -----------"

    #region "----------  Methods -----------"

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN01_4A;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqCounterPartyName.SetRequiredField(lblCounterPartyName, txtCounterPartyName, btnSAVE);
        reqFinancialNameTH.SetRequiredField(lblFinancialNameTH, txtFinancialNameTH, btnSAVE);
        reqFinancialNameEN.SetRequiredField(lblFinancialNameEN, txtFinancialNameEN, btnSAVE);
        reqFinancialType.SetRequiredField(lblFinancialType, ddlFinancialType, btnSAVE);
        reqPartyType.SetRequiredField(lblPartyType, ddlPartyType, btnSAVE);
        reqGLAccountBondAdd.SetRequiredField(lblGLAccountBondAdd, txtGLAccountBondAdd, btnSAVE);
        reqGLAccountBondMinus.SetRequiredField(lblGLAccountBondMinus, txtGLAccountBondMinus, btnSAVE);
    }

    private void BindData(int ID)
    {
        List<CounterParty> counterParty = new List<CounterParty>();
        DataTable dt = new SPR_COUNTERPARTY().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, counterParty);
            txtTransactionDate.Text = counterParty[0].TransactionDate.DateToString();
            txtCounterPartyName.Text = counterParty[0].CounterPartyName;
            txtFinancialNameTH.Text = counterParty[0].FinancialNameTH;
            txtFinancialNameEN.Text = counterParty[0].FinancialNameEN;
            ddlFinancialType.SelectedValue = counterParty[0].FinancialTypeID.IntToString();
            ddlPartyType.SelectedValue = counterParty[0].PartyTypeID.IntToString();
            txtGLAccountBondAdd.Text = counterParty[0].GLAccountBondAdd;
            txtGLAccountBondMinus.Text = counterParty[0].GLAccountBondMinus;
        }
    }

    private void SaveData()
    {
        try
        {
            CounterParty counterParty = new CounterParty();
            counterParty.CounterPartyID = Convert.ToInt32(Request.QueryString["id"].ToString());
            counterParty.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            counterParty.CounterPartyName = txtCounterPartyName.Text;
            counterParty.FinancialNameTH = txtFinancialNameTH.Text;
            counterParty.FinancialNameEN = txtFinancialNameEN.Text;
            counterParty.FinancialTypeID = ddlFinancialType.SelectedValue.AsInt32();
            counterParty.PartyTypeID = ddlPartyType.SelectedValue.AsInt32();
            counterParty.GLAccountBondAdd = txtGLAccountBondAdd.Text;
            counterParty.GLAccountBondMinus = txtGLAccountBondMinus.Text;
            counterParty.SetStandardField();
            int result = new SPR_COUNTERPARTY().UPDATE(counterParty);

            if (result > 0)
            {
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN16_2E.aspx?&id={0}", counterParty.CounterPartyID)));
            }
            else
            {
                if (result == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindDDL()
    {
        ddlFinancialType.SetData(DDL_TYPE.FinancialType, "", false, true);
        ddlPartyType.SetData(DDL_TYPE.PartyType, "", false, true);
    }
    #endregion "---------- Methods -----------"
}